﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MYQQX
{
    public partial class CardsList : UserControl
    {
        /// <summary>
        /// 是否显示卡牌
        /// </summary>
        public bool ShowCard { get; set; } = true;

        public CardSeason NeedSeason = CardSeason.All;

        public Action<Card> ShowCardAct = null;

        public CardsList()
        {
            InitializeComponent();
        }

        internal List<Card> cardList = new List<Card>();

        private Control ChosePanel = null;
        public Card ChoseCard { get; private set; }

        public void AddCard(List<Card> cards)
        {
            foreach (var cd in cards)
            {
                AddCard(cd);
            }
        }

        public void AddCard(Card card)
        {
            cardList.Add(card);
            Panel panel = new Panel();
            panel.Paint += Panel_Paint;
            panel.Height = 20;
            panel.Width = this.Width - 10;
            panel.BackColor = card.Color;
            panel.Location = new Point(0, cardList.IndexOf(card) * 20);
            panel.Tag = card;

            Label label = new Label();
            label.Text = card.Name;
            label.AutoSize = false;
            label.Width = panel.Width - 50;
            label.Height = panel.Height -2;
            label.TextAlign = ContentAlignment.MiddleLeft;
            label.Location = new Point(30, 1);
            label.Tag = card;
            if (!ShowCard)
            {
                label.Text = "口口口口口口";
                panel.BackColor = ColorTranslator.FromHtml("#663333");
            }

            panel.Controls.Add(label);

            label.Click += Card_Click;
            panel.Click += Card_Click;

            card.ShowControl = panel;
            this.Controls.Add(panel);
            Refresh();
        }

        private void Card_Click(object sender, EventArgs e)
        {
            if (!ShowCard)
                return;

            var control = sender as Control;
            var card = control.Tag as Card;

            if (NeedSeason != CardSeason.All
                && card.JiJie != NeedSeason)
                return;

            var panel = card.ShowControl;
            if (panel.Location.X == 0)
            {
                if (ChosePanel != null)
                    ChosePanel.Location = new Point(0, ChosePanel.Location.Y);

                ChosePanel = panel;
                panel.Location = new Point(10, panel.Location.Y);
                ChoseCard = card;
                ShowCardAct?.Invoke(card);
            }
            else
            {
                ChosePanel = null;
                ChoseCard = null;
                panel.Location = new Point(0, panel.Location.Y);
            }
        }

        internal void OpenCard(int count)
        {
            if (!ShowCard)
            {
                if (count < cardList.Count)
                {
                    var rlist = GetRandom(cardList.Count, count);
                    foreach (var i in rlist)
                    {
                        var card = cardList[i];
                        var label = card.ShowControl.Controls[0] as Label;
                        label.Text = card.Name;
                        label.BackColor = card.Color;
                    }
                }
                else
                {
                    foreach (var card in cardList)
                    {
                        var label = card.ShowControl.Controls[0] as Label;
                        label.Text = card.Name;
                        label.BackColor = card.Color;
                    }
                }
            }
        }

        private List<int> GetRandom(int max, int count)
        {
            Random rand = new Random(Guid.NewGuid().GetHashCode());
            List<int> rList = new List<int>();
            List<int> aList = new List<int>();
            for (int i = 0; i < max; i++)
            {
                aList.Add(i);
            }
            for (int i = 0; i < count; i++)
            {
                var index = rand.Next(aList.Count);
                rList.Add(aList[index]);
                aList.RemoveAt(index);
            }
            return rList;
        }

        internal void UseChose()
        {
            if (this.ChoseCard == null)
                return;

            var index = this.cardList.IndexOf(this.ChoseCard);
            this.Controls.Remove(this.ChoseCard.ShowControl);
            this.cardList.Remove(this.ChoseCard);
            for (int i = index; i < cardList.Count; i++)
            {
                var card = cardList[i];
                card.ShowControl.Location = new Point(0, i * 20);
            }
            this.ChoseCard = null;
            this.ChosePanel = null;
            Refresh();
        }

        private void Panel_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder(e.Graphics, (sender as Panel).ClientRectangle,
            Color.DimGray, 1, ButtonBorderStyle.Solid, //左边
            Color.DimGray, 1, ButtonBorderStyle.Solid, //上边
            Color.DimGray, 1, ButtonBorderStyle.Solid, //右边
            Color.DimGray, 1, ButtonBorderStyle.Solid);//底边
        }

        internal void setChose(Card handChose)
        {
            foreach (var card in this.cardList)
            {
                if (card.Name == handChose.Name)
                {
                    this.ChoseCard = card;
                    break;
                }
            }
        }
    }
}
